using System;
using System.Reflection;
using System.Windows.Forms;
using NUnit.Framework;
using ShadeTree.Binding.Validation;

namespace ShadeTree.Binding.Tests
{
    public static class TestUtility
    {
        public static void FireEvent(object control, string eventName, params object[] args)
        {
            MethodInfo minfo =
                control.GetType().GetMethod("On" + eventName,
                                            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            ParameterInfo[] param = minfo.GetParameters();
            Type parameterType = param[0].ParameterType;
            minfo.Invoke(control, new[] { Activator.CreateInstance(parameterType) });
        }

        public static void Click(Control control)
        {
            FireEvent(control, "Click");
        }

        public static void AssertRequiredField<T>(params string[] properties)
        {
            string message = "";
            Type type = typeof(T);
            foreach (string propertyName in properties)
            {
                PropertyInfo property = type.GetProperty(propertyName);
                Attribute attribute = Attribute.GetCustomAttribute(property, typeof(RequiredAttribute));
                if (attribute == null)
                {
                    message += string.Format("Property {0} does not have a Required attribute\n", propertyName);
                }
            }

            if (!string.IsNullOrEmpty(message))
            {
                Assert.Fail(message);
            }
        }
    }
}